package inferenceEngine.types;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.dom4j.Element;

public class LinguisticVariable {

	private ArrayList<LinguisticValue> values = new ArrayList<LinguisticValue>();
	private Color[] colors = new Color[5];
	private String name = "";
	private String attribute = "";
	
	public String getName(){
		return name;
	}
	public LinguisticVariable(Element e) {
		colors[0] = Color.BLACK;
		colors[1] = Color.RED;
		colors[2] = Color.BLUE;
		colors[3] = Color.GRAY;
		colors[4] = Color.GREEN;
		this.name = e.attributeValue("name");
		if( e.attribute("attribute") != null ){
			this.setAttribute(e.attributeValue("attribute"));
		}
		for( Object ele : e.elements("value") ){
			values.add( new LinguisticValue(  (Element) ele ) );
		}
	}
	
	public ArrayList<String> getValueNames(){
		ArrayList<String> names = new ArrayList<String>();
		for( LinguisticValue value : values ){
			names.add( value.getName() );
		}
		return names;
	}
	
	public ArrayList<LinguisticValue> getLinguisticValues(){
		return values;
	}
	
	public void drawGraph( Graphics g ){
		double maxX = 0;
		double minX = Double.MAX_VALUE;
		for( LinguisticValue value : values ){
			if( value.getMaxX()>maxX ){
				maxX = value.getMaxX();
			}
			if( value.getMinX()<minX ){
				minX = value.getMinX();
			}
		}
		maxX +=  ( maxX - minX )*0.1;
		minX -= ( maxX - minX )*0.1;
		for( int i=0 ; i< values.size() ;i++ ){
			LinguisticValue value = values.get(i);
			Color c = Color.BLACK;
			if( i>4 ){
				Random randomer = new Random();
				c = new Color( randomer.nextInt(255), randomer.nextInt(255), randomer.nextInt(255) );
			}else{
				c = colors[i];
			}
			value.drawLine(g, minX, maxX, c );
		}
	}
	public String getAttribute() {
		return attribute.equals("")? name : attribute ;
	}
	public void setAttribute(String attribute) {
		this.attribute = attribute;
	}

}
